home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter9 / trackbar.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  11KB  |  293 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "trackbar.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "Track Bar"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107.  
  108.  
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static HWND hTrack = NULL;
  112.  
  113.    switch( uMsg )
  114.    {
  115.       case WM_CREATE :
  116.               InitCommonControls();
  117.  
  118.               // Create the edit control.
  119.               //.........................
  120.               hTrack = CreateWindowEx( 0, TRACKBAR_CLASS, "",
  121.                                        WS_CHILD | WS_VISIBLE |
  122.                                        TBS_ENABLESELRANGE | TBS_AUTOTICKS,
  123.                                        40, 20, 200, 30, hWnd, 
  124.                                        (HMENU)IDC_TRACKBAR, hInst, NULL );
  125.  
  126.               if ( hTrack )
  127.                  SendMessage( hTrack, TBM_SETRANGE, TRUE, MAKELPARAM(0,10) );
  128.  
  129.               break;
  130.  
  131.  
  132.       case WM_COMMAND :
  133.               switch( LOWORD( wParam ) )
  134.               {
  135.                  case IDM_PROPERTIES :
  136.                         DialogBox( hInst, "OptionsDlg", hWnd, (DLGPROC)Options );
  137.                         break;
  138.  
  139.                  case IDM_RESET :
  140.                         SendMessage( hTrack, TBM_SETRANGE, TRUE, MAKELPARAM(0,10) );
  141.                         SendMessage( hTrack, TBM_CLEARSEL, TRUE, 0 );
  142.                         SendMessage( hTrack, TBM_CLEARTICS, TRUE, 0 );
  143.                         break;
  144.  
  145.                  case IDM_ABOUT :
  146.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  147.                         break;
  148.  
  149.                  case IDM_EXIT :
  150.                         DestroyWindow( hWnd );
  151.                         break;
  152.               }
  153.               break;
  154.       
  155.       case WM_DESTROY :
  156.               PostQuitMessage(0);
  157.               break;
  158.  
  159.       default :
  160.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  161.    }
  162.  
  163.    return( 0L );
  164. }
  165.  
  166.  
  167. LRESULT CALLBACK Options( HWND hDlg,           
  168.                           UINT message,        
  169.                           WPARAM wParam,       
  170.                           LPARAM lParam)
  171. {
  172.    switch (message) 
  173.    {
  174.        case WM_INITDIALOG: 
  175.                {
  176.                   HWND   hTrack;
  177.                   DWORD  dwLineSize;
  178.                   DWORD  dwNumTics;
  179.                   DWORD  dwPageSize;
  180.                   DWORD  dwPos;
  181.                   DWORD  dwThSize;
  182.                   DWORD  dwMaxRange;
  183.                   DWORD  dwMinRange;
  184.                   DWORD  dwSelStart;
  185.                   DWORD  dwSelEnd;
  186.  
  187.                   // Retrieve the trackbar handle.
  188.                   //...............................
  189.                   hTrack = GetDlgItem( GetParent( hDlg ), IDC_TRACKBAR );
  190.  
  191.                   // Retrieve the information from the trackbar.
  192.                   //............................................
  193.                   dwLineSize = SendMessage( hTrack, TBM_GETLINESIZE, 0, 0 );
  194.                   dwNumTics  = SendMessage( hTrack, TBM_GETNUMTICS, 0, 0 );
  195.                   dwPageSize = SendMessage( hTrack, TBM_GETPAGESIZE, 0, 0 );
  196.                   dwPos      = SendMessage( hTrack, TBM_GETPOS, 0, 0 );
  197.                   dwMinRange = SendMessage( hTrack, TBM_GETRANGEMIN, 0, 0 );
  198.                   dwMaxRange = SendMessage( hTrack, TBM_GETRANGEMAX, 0, 0 );
  199.                   dwSelStart = SendMessage( hTrack, TBM_GETSELSTART, 0, 0 );
  200.                   dwSelEnd   = SendMessage( hTrack, TBM_GETSELEND, 0, 0 );
  201.                   dwThSize   = SendMessage( hTrack, TBM_GETTHUMBLENGTH, 0, 0 );
  202.  
  203.                   // Initialize the dialog controls with the values.
  204.                   //................................................
  205.                   SetDlgItemInt( hDlg, IDC_LINESIZE, dwLineSize, FALSE );
  206.                   SetDlgItemInt( hDlg, IDC_TICKFREQ, 
  207.                                  (dwMaxRange-dwMinRange)/(dwNumTics-1), FALSE );
  208.                   SetDlgItemInt( hDlg, IDC_PAGESIZE, dwPageSize, FALSE );
  209.                   SetDlgItemInt( hDlg, IDC_POS, dwPos, FALSE );
  210.                   SetDlgItemInt( hDlg, IDC_THUMBSIZE, dwThSize, FALSE );
  211.                   SetDlgItemInt( hDlg, IDC_RANGE1, dwMinRange, FALSE );
  212.                   SetDlgItemInt( hDlg, IDC_RANGE2, dwMaxRange, FALSE );
  213.                   SetDlgItemInt( hDlg, IDC_SEL1, dwSelStart, FALSE );
  214.                   SetDlgItemInt( hDlg, IDC_SEL2, dwSelEnd, FALSE );
  215.                }
  216.                return (TRUE);
  217.  
  218.        case WM_COMMAND:                              
  219.                if ( LOWORD(wParam) == IDOK )
  220.                {
  221.                   HWND   hTrack;
  222.                   DWORD  dwLineSize;
  223.                   DWORD  dwNumTics;
  224.                   DWORD  dwPageSize;
  225.                   DWORD  dwPos;
  226.                   DWORD  dwThSize;
  227.                   DWORD  dwMaxRange;
  228.                   DWORD  dwMinRange;
  229.                   DWORD  dwSelStart;
  230.                   DWORD  dwSelEnd;
  231.  
  232.                   // Retrieve the trackbar handle.
  233.                   //...............................
  234.                   hTrack = GetDlgItem( GetParent( hDlg ), IDC_TRACKBAR );
  235.  
  236.                   // Retrieve the new settings from the dialog.
  237.                   //...........................................
  238.                   dwLineSize = GetDlgItemInt( hDlg, IDC_LINESIZE, NULL, FALSE );
  239.                   dwNumTics  = GetDlgItemInt( hDlg, IDC_TICKFREQ, NULL, FALSE );
  240.                   dwPageSize = GetDlgItemInt( hDlg, IDC_PAGESIZE, NULL, FALSE );
  241.                   dwPos      = GetDlgItemInt( hDlg, IDC_POS, NULL, FALSE );
  242.                   dwThSize   = GetDlgItemInt( hDlg, IDC_THUMBSIZE, NULL, FALSE );
  243.                   dwMinRange = GetDlgItemInt( hDlg, IDC_RANGE1, NULL, FALSE );
  244.                   dwMaxRange = GetDlgItemInt( hDlg, IDC_RANGE2, NULL, FALSE );
  245.                   dwSelStart = GetDlgItemInt( hDlg, IDC_SEL1, NULL, FALSE );
  246.                   dwSelEnd   = GetDlgItemInt( hDlg, IDC_SEL2, NULL, FALSE );
  247.  
  248.                   // Set the new attributes of the trackbar control.
  249.                   //................................................
  250.                   SendMessage( hTrack, TBM_SETLINESIZE, 0, dwLineSize );
  251.                   SendMessage( hTrack, TBM_SETTICFREQ, dwNumTics, 0 );
  252.                   SendMessage( hTrack, TBM_SETPAGESIZE, 0, dwPageSize );
  253.                   SendMessage( hTrack, TBM_SETPOS, TRUE, dwPos );
  254.                   SendMessage( hTrack, TBM_SETRANGEMIN, TRUE, dwMinRange );
  255.                   SendMessage( hTrack, TBM_SETRANGEMAX, TRUE, dwMaxRange );
  256.                   SendMessage( hTrack, TBM_SETSELSTART, TRUE, dwSelStart );
  257.                   SendMessage( hTrack, TBM_SETSELEND, TRUE, dwSelEnd );
  258.                   SendMessage( hTrack, TBM_SETTHUMBLENGTH, dwThSize, 0 );
  259.  
  260.                   EndDialog( hDlg, IDOK );        
  261.                }
  262.                else if ( LOWORD(wParam) == IDCANCEL )    
  263.                   EndDialog( hDlg, IDCANCEL );        
  264.                break;
  265.    }
  266.  
  267.    return (FALSE); 
  268. }
  269.  
  270.  
  271. LRESULT CALLBACK About( HWND hDlg,           
  272.                         UINT message,        
  273.                         WPARAM wParam,       
  274.                         LPARAM lParam)
  275. {
  276.    switch (message) 
  277.    {
  278.        case WM_INITDIALOG: 
  279.                return (TRUE);
  280.  
  281.        case WM_COMMAND:                              
  282.                if (   LOWORD(wParam) == IDOK         
  283.                    || LOWORD(wParam) == IDCANCEL)    
  284.                {
  285.                        EndDialog(hDlg, TRUE);        
  286.                        return (TRUE);
  287.                }
  288.                break;
  289.    }
  290.  
  291.    return (FALSE); 
  292. }
  293.